package com.hzdl.car.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class CheckAdminIsLogin implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        // 检查管理员是否登录
        // 获取请求的地址
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String uri = request.getRequestURI();
        if(!uri.startsWith("/car/user")&& !uri.startsWith("/car/static/") ){
            // 获取登录的管理员信息
            Object obj = request.getSession().getAttribute("session_user");
            if(obj == null){
                // 没有登录，转到登录页面
                response.sendRedirect("/car/user/loginInit");
                return;

            }

        }
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
